package main

import "fmt"

/**
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.

![20190522155852868275739.png](http://cdn.bsummer.cn/20190522155852868275739.png)

思路:
面积等于长乘宽
首先以最大长度求出面积，记作初始面积，然后有取向性的减小长度(哪边宽度小，缩短哪边)，求出面积与初始面积比较取大者，继续循环减小长度取面积，最终得到所有可能大的容器中最大的面积
*/

func maxArea(height []int) int {
	maxArea := 0
	left, right := 0, len(height)-1

	for left < right {
		hl, hr := height[left], height[right]
		maxArea = max(maxArea, (right-left)*min(hl, hr))

		if hl < hr {
			left++
		} else {
			right--
		}
	}

	return maxArea
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func main() {
	fmt.Println(maxArea([]int{1, 8, 6, 2, 5, 4, 8, 3, 7}))
}
